package com.qcby.mycommunity_003.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qcby.mycommunity_003.entity.Community;
import com.qcby.mycommunity_003.entity.InOutRecord;
import com.qcby.mycommunity_003.entity.Person;
import com.qcby.mycommunity_003.mapper.InOutRecordMapper;
import com.qcby.mycommunity_003.mapper.PersonMapper;
import com.qcby.mycommunity_003.service.CommunityService;
import com.qcby.mycommunity_003.service.InOutRecordService;
import com.qcby.mycommunity_003.service.PersonService;
import com.qcby.mycommunity_003.vo.ChartVO;
import com.qcby.mycommunity_003.vo.InOutListVo;
import com.qcby.mycommunity_003.vo.InOutRecordResult;
import com.qcby.mycommunity_003.vo.PageVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author admin
 * @since 2024-03-25
 */
@Service
public class InOutRecordServiceImpl extends ServiceImpl<InOutRecordMapper, InOutRecord> implements InOutRecordService {

    @Autowired
    private InOutRecordMapper inOutRecordMapper;
    @Autowired
    private PersonService personService;
    @Autowired
    private PersonMapper personMapper;
    @Autowired
    private CommunityService communityService;
    @Override
    public Map charts() {
        List<ChartVO> charts = inOutRecordMapper.charts();
        List<String> names=new ArrayList<>();
        List<Integer> nums=new ArrayList<>();
        List<ChartVO> list=new ArrayList<>();
        for (ChartVO chart : charts) {
            names.add(chart.getName());
            nums.add(chart.getValue());
            list.add(chart);
        }
        HashMap<String,List> map=new HashMap<>();
        map.put("names",names);
        map.put("nums",nums);
        map.put("list",list);
        return map;
    }

    /**
     * 分页逻辑
     * @param listVo
     * @return
     */
    @Override
    public PageVo getlist(InOutListVo listVo) {
        //对page对象赋值时先是当前页码，再是每页的数量
        Page<InOutRecord> page=new Page<>();
        page.setCurrent(listVo.getPage());
        page.setSize(listVo.getLimit());
        QueryWrapper<Person> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.like(StringUtils.isNotBlank(listVo.getUserName()),"user_name",listVo.getUserName());
        //注意按照名字查找，找到的不是一个
        List<Person> people = personMapper.selectList(queryWrapper1);
        List<Integer> p_list=new ArrayList<>();
        for (Person person : people) {
            p_list.add(person.getPersonId());
        }
        QueryWrapper<InOutRecord> queryWrapper=new QueryWrapper<>();
        queryWrapper.in(p_list.size()>0,"person_id",p_list)
//        queryWrapper.between(StringUtils.isNotBlank(listVo.getStartDate()) && StringUtils.isNotBlank(listVo.getEndDate()), "in_time", listVo.getStartDate(), listVo.getEndDate());
                    .ge(StringUtils.isNotBlank(listVo.getStartDate()),"in_time",listVo.getStartDate())
                    .le(StringUtils.isNotBlank(listVo.getEndDate()),"out_time",listVo.getEndDate());
        page= inOutRecordMapper.selectPage(page, queryWrapper);
        PageVo pageVo=new PageVo();
        pageVo.setTotalCount(page.getTotal());
        pageVo.setCurrPage(page.getCurrent());
        pageVo.setPageSize(page.getSize());
        pageVo.setTotalPage(page.getPages());
        ArrayList<InOutRecordResult> list=new ArrayList();
        for (InOutRecord record : page.getRecords()) {
            InOutRecordResult inOutRecord=new InOutRecordResult();
            BeanUtils.copyProperties(record,inOutRecord);
            //查询小区名字
            Community byId = communityService.getById(record.getCommunityId());
            inOutRecord.setCommunityName(byId.getCommunityName());
            //查询人员信息以及居住在哪里
            Person p = personService.getById(record.getPersonId());
            inOutRecord.setHouseNo(p.getHouseNo());
            inOutRecord.setUserName(p.getUserName());
            inOutRecord.setTermName(p.getTermName());
            list.add(inOutRecord);
        }
        //忘记把list赋值了？？？？？
        pageVo.setList(list);
        return pageVo;
    }
}
